package com.lzy.srb.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzy.srb.core.enums.BorrowerStatusEnum;
import com.lzy.srb.core.enums.IntegralEnum;
import com.lzy.srb.core.mapper.BorrowerAttachMapper;
import com.lzy.srb.core.mapper.UserInfoMapper;
import com.lzy.srb.core.mapper.UserIntegralMapper;
import com.lzy.srb.core.pojo.entity.Borrower;
import com.lzy.srb.core.mapper.BorrowerMapper;
import com.lzy.srb.core.pojo.entity.BorrowerAttach;
import com.lzy.srb.core.pojo.entity.UserInfo;
import com.lzy.srb.core.pojo.entity.UserIntegral;
import com.lzy.srb.core.pojo.vo.BorrowerApprovalVO;
import com.lzy.srb.core.pojo.vo.BorrowerAttachVO;
import com.lzy.srb.core.pojo.vo.BorrowerDetailVO;
import com.lzy.srb.core.pojo.vo.BorrowerVO;
import com.lzy.srb.core.service.BorrowerAttachService;
import com.lzy.srb.core.service.BorrowerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzy.srb.core.service.DictService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 借款人 服务实现类
 * </p>
 *
 * @author lzy
 * @since 2023-02-23
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class BorrowerServiceImpl extends ServiceImpl<BorrowerMapper, Borrower> implements BorrowerService {

    @Autowired
    UserInfoMapper userInfoMapper;

    @Autowired
    BorrowerAttachMapper borrowerAttachMapper;

    @Autowired
    DictService dictService;

    @Autowired
    BorrowerAttachService borrowerAttachService;

    @Autowired
    UserIntegralMapper userIntegralMapper;

    @Override
    public void saveBorrowerVOByUserId(BorrowerVO borrowerVO, Long userId) {


        UserInfo userInfo = userInfoMapper.selectById(userId);

        Borrower borrower = new Borrower();
        BeanUtils.copyProperties(borrowerVO,borrower);
        borrower.setUserId(userId);
        borrower.setName(userInfo.getName());
        borrower.setIdCard(userInfo.getIdCard());
        borrower.setMobile(userInfo.getMobile());
        borrower.setStatus(BorrowerStatusEnum.AUTH_RUN.getStatus());
//        borrower.setStatus(userInfoMapper.selectById(userId).getStatus());
        baseMapper.insert(borrower);

        List<BorrowerAttach> borrowerAttachList = borrowerVO.getBorrowerAttachList();
        borrowerAttachList.forEach(item->{
            item.setBorrowerId(borrower.getId());
            borrowerAttachMapper.insert(item);
        });

        userInfo.setBorrowAuthStatus(BorrowerStatusEnum.AUTH_RUN.getStatus());
        userInfoMapper.updateById(userInfo);

    }

    @Override
    public Integer getStatusByUserId(Long userId) {

        QueryWrapper<Borrower> wrapper = new QueryWrapper<>();
        wrapper.select("status").eq("user_id",userId);
        List<Object> objects = baseMapper.selectObjs(wrapper);
        List<Borrower> borrowers = baseMapper.selectList(wrapper);
        if(objects.size() == 0){
            return BorrowerStatusEnum.NO_AUTH.getStatus();
        }

        Integer status = (Integer) objects.get(0);
        return status;
    }

    @Override
    public IPage<Borrower> listPage(Page<Borrower> pageParam, String keyword) {
        if(StringUtils.isBlank(keyword)){
            return baseMapper.selectPage(pageParam,null);
        }else{
            QueryWrapper<Borrower> wrapper = new QueryWrapper<>();
            wrapper
                    .like("name",keyword)
                    .or().like("id_card",keyword)
                    .or().like("mobile",keyword)
                    .orderByAsc("id");
            return baseMapper.selectPage(pageParam,wrapper);
        }
    }

    @Override
    public BorrowerDetailVO getBorrowerDetailVOById(Long id) {

        //获取借款人信息
        Borrower borrower = baseMapper.selectById(id);

        //填充基本借款人信息
        BorrowerDetailVO bdv = new BorrowerDetailVO();
        BeanUtils.copyProperties(borrower,bdv);

        //婚否
        bdv.setMarry(borrower.getIsMarry()?"是":"否");
        //性别
        bdv.setSex(borrower.getSex()==1?"男":"女");

        //下拉列表
        bdv.setEducation(dictService.getNameByParentDictCodeAndValue("education",borrower.getEducation()));
        bdv.setIndustry(dictService.getNameByParentDictCodeAndValue("industry",borrower.getIndustry()));
        bdv.setIncome(dictService.getNameByParentDictCodeAndValue("income",borrower.getIncome()));
        bdv.setReturnSource(dictService.getNameByParentDictCodeAndValue("returnSource",borrower.getReturnSource()));
        bdv.setContactsRelation(dictService.getNameByParentDictCodeAndValue("relation",borrower.getContactsRelation()));

        //审批状态
        Integer status = borrower.getStatus();
        String msg = BorrowerStatusEnum.getMsgByStatus(status);
        bdv.setStatus(msg);

        //附件列表
        List<BorrowerAttachVO> borrowerAttachVOSList = borrowerAttachService.selectBorrowerAttachVOList(id);
        bdv.setBorrowerAttachVOList(borrowerAttachVOSList);

        return bdv;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void approval(BorrowerApprovalVO borrowerApprovalVO) {

        //获取借款额度申请id
        Long borrowerId = borrowerApprovalVO.getBorrowerId();

        //获取借款额度申请对象
        Borrower borrower = baseMapper.selectById(borrowerId);
        borrower.setStatus(borrowerApprovalVO.getStatus());

        baseMapper.updateById(borrower);
        Long userId = borrower.getUserId();

        UserInfo userInfo = userInfoMapper.selectById(userId);

        Integer integral = userInfo.getIntegral();

        UserIntegral userIntegral = new UserIntegral();
        userIntegral.setUserId(userId);
        userIntegral.setIntegral(borrowerApprovalVO.getInfoIntegral());
        userIntegral.setContent("借款人基本信息");
        userIntegralMapper.insert(userIntegral);
        int currentIntegral = integral + borrowerApprovalVO.getInfoIntegral();

        if(borrowerApprovalVO.getIsIdCardOk()){
            userIntegral = new UserIntegral();
            userIntegral.setUserId(userId);
            userIntegral.setIntegral(IntegralEnum.BORROWER_IDCARD.getIntegral());
            userIntegral.setContent(IntegralEnum.BORROWER_IDCARD.getMsg());
            userIntegralMapper.insert(userIntegral);
            currentIntegral+=IntegralEnum.BORROWER_IDCARD.getIntegral();
        }

        if(borrowerApprovalVO.getIsHouseOk()){
            userIntegral = new UserIntegral();
            userIntegral.setUserId(userId);
            userIntegral.setIntegral(IntegralEnum.BORROWER_HOUSE.getIntegral());
            userIntegral.setContent(IntegralEnum.BORROWER_HOUSE.getMsg());
            userIntegralMapper.insert(userIntegral);
            currentIntegral+=IntegralEnum.BORROWER_HOUSE.getIntegral();
        }

        if(borrowerApprovalVO.getIsCarOk()){
            userIntegral = new UserIntegral();
            userIntegral.setUserId(userId);
            userIntegral.setIntegral(IntegralEnum.BORROWER_CAR.getIntegral());
            userIntegral.setContent(IntegralEnum.BORROWER_CAR.getMsg());
            userIntegralMapper.insert(userIntegral);
            currentIntegral+=IntegralEnum.BORROWER_CAR.getIntegral();
        }

        userInfo.setIntegral(currentIntegral);
        userInfo.setBorrowAuthStatus(borrowerApprovalVO.getStatus());
        userInfoMapper.updateById(userInfo);
    }
}
